<!--关联设备-->
<template>
  <div class="ele-body">
    <a-divider orientation="left">可控制的设备列表</a-divider>
    <a-table :columns="linkDeviceColumns" :data-source="linkDeviceList" :pagination="false" bordered >
      <template #bodyCell="{ column, record }">
        <!-- table操作栏按钮 -->
        <template v-if="column.key === 'action'">
          <a-space>
            <a-popconfirm title="确定要解绑吗？" @confirm="unbind(record)">
              <a class="ele-text-success">解绑</a>
            </a-popconfirm>
          </a-space>
        </template>
      </template>
    </a-table>

    <a-divider orientation="left">关联设备</a-divider>
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>

            <a-form-item label="设备类型:" name="dceId">
              <a-select
                show-search
                v-model:value="where.dceId"
                placeholder="请选择设备类型"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
              >
                <a-select-option v-for="item in devicetypeList" :key="item.deviceId" >
                  {{ item.deviceName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item label="所属牧场:" name="orgId">
              <a-select
                show-search
                v-model:value="where.orgId"
                placeholder="请选择所属牧场"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @change="selectRanch"
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>


            <a-form-item label="所属牛舍:" name="oxtId">
              <a-select
                show-search
                v-model:value="where.oxtId"
                placeholder="请选择所属牛舍"
                style="width: 200px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
              >
                <a-select-option v-for="item in oxtallList" :key="item.oxtallId" >
                  {{ item.oxtallName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="recordId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space v-if="record.switchMappingDeviceRecordDTO != null">
                <a-popconfirm v-if="record.switchMappingDeviceRecordDTO.recordId == this.data.recordId" title="确定要解绑吗？" @confirm="unbind(record)">
                  <a class="ele-text-success">解绑</a>
                </a-popconfirm>
                <a-tag v-else color="red">已绑定其他</a-tag>
              </a-space>
              <a-space v-else>
                <a-popconfirm title="确定要绑定吗？" @confirm="bind(record)">
                  <a class="ele-text-danger">绑定</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

</template>

<script>
import { message } from 'ant-design-vue';
import { DeviceRecordApi } from '@/api/property/DeviceRecordApi';
import {DeviceApi} from "@/api/property/DeviceApi";
import {RanchApi} from "@/api/property/RanchApi";
import {OxtallApi} from "@/api/property/OxtallApi";
import {DeviceSwitchMappingApi} from "@/api/property/DeviceSwitchMappingApi"
export default {
  name: 'DeviceLuoxunSimplephaseLinkDevice',
  components: {
  },
  props: {
    data: Object
  },
  async mounted() {
  },
  async created() {
    DeviceApi.list({}).then(resp => {
      let _deviceList = (resp ? resp.data : null) || [];
      let _formatList = [];
      _deviceList.forEach(function (element) {
        if (element.templateCode != 'luoxun1phase' && element.templateCode != 'luoxun3phase') {
          _formatList.push(element)
        }
      });
      this.devicetypeList = _formatList;
    });

    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      this.ranchList = _ranchList;
      if (_ranchList.length > 0) {
        OxtallApi.list({"orgId" : _ranchList[0].ranchId}).then(result => {
          this.oxtallList = (result ? result.data : null) || [];
        }).catch((err) => {
          console.log(err)
        });
      }
    });

    this.listBySwitchRecordId();
  },
  data() {
    return {
      //已关联设备
      linkDeviceColumns : [
        {
          title: '设备ID',
          dataIndex: 'recordId'
        },
        {
          title: '设备类型',
          dataIndex: 'deviceName'
        },
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '牛舍',
          dataIndex: 'oxtallName'
        },
        {
          title: '区域',
          dataIndex: 'areaName'
        },
        {
          title: '操作',
          key: 'action',
          width: 140,
          align: 'center'
        }
      ],
      // 表格列配置
      columns: [
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '牛舍',
          dataIndex: 'oxtallName'
        },
        {
          title: '区域',
          dataIndex: 'areaName'
        },
        {
          title: '设备类型',
          dataIndex: 'deviceName'
        },
        {
          title: '设备ID',
          dataIndex: 'recordId'
        },
        {
          title: '操作',
          key: 'action',
          width: 140,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {isRelateSwitch : 0},
      // 表格选中数据
      selection: [],
      devicetypeList : [],
      ranchList : [],
      oxtallList : [],
      linkDeviceList : []
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
      this.listBySwitchRecordId();
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    reset() {
      this.where.oxtId = '';
      this.where.orgId = '';
      this.where.dceId = '';
      this.where.isRelateSwitch = 0;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    async unbind(row) {
      let addParams = {
        recordId :  row.recordId,
        switchId : this.data.recordId,
      };
      const result = await DeviceSwitchMappingApi.unbind(addParams);
      message.success(result.message);
      this.reload();
    },
    async bind(row)
    {
      let addParams = {
        recordId :  row.recordId,
        switchId : this.data.recordId,
        switchCode : this.data.templateCode
      };
      const result = await DeviceSwitchMappingApi.add(addParams);
      message.success(result.message);
      this.reload();
    },

    listBySwitchRecordId()
    {
      DeviceRecordApi.listBySwitchRecordId({"switchRecordId" : this.data.recordId}).then(resp => {
        this.linkDeviceList = (resp ? resp.data : null) || [];
        console.log(this.linkDeviceList);
      });
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceRecordApi.findPageWithSwitch({ ...where, ...orders, pageNo: page, pageSize: limit });
    },

    /**
     * 查询牛舍
     * @param ranchId
     */
    selectRanch(ranchId) {
      this.oxtallList = [];
      this.where.oxtId = undefined;
      OxtallApi.list({"orgId" : ranchId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    }

  }
};
</script>
